package com.coding.controller;


import com.coding.dto.RestBean;
import com.coding.pojo.Users;
import com.coding.service.UserService;
import com.coding.util.ResponseDTO;
import com.coding.vo.LoginVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;


    //输入手机及密码登录
    @RequestMapping("login")
    public RestBean<Users> login(@RequestBody LoginVo loginVo) {
            return userService.login(loginVo);
    }

    //通过手机验证码登录
    @RequestMapping("loginAndCode")
    public ResponseDTO loginAndCode(@RequestBody LoginVo vo) {
        // 从Redis中获取存储的验证码
        String storedCode = (String) redisTemplate.opsForValue().get("code_" + vo.getPhoneNumber());
        System.out.println("****loginAndCodeController*******" + storedCode + "******" + vo.getPhoneNumber());
        // 检查存储的验证码是否存在以及是否与用户输入的验证码匹配
        if (storedCode != null && vo.getCode().equals(storedCode)){
            return userService.loginAndCode(vo);
        } else {
            return ResponseDTO.error("验证码输入错误");
        }
    }

    //通过邮箱和密码登录
    @RequestMapping("emailAndpwd")
    public ResponseDTO emailAndpwd(@RequestBody LoginVo vo) {
            return userService.emailAndpwd(vo);
    }

    //通过邮箱验证码登录
    @RequestMapping("mailAndcode")
    public RestBean<Users> mailAndcode(@RequestBody LoginVo vo) {
        // 从Redis中获取存储的验证码
        String storedCode = (String) redisTemplate.opsForValue().get("emailcode" + vo.getEmail());
        // 检查存储的验证码是否存在以及是否与用户输入的验证码匹配
        if (storedCode != null && vo.getCode().equals(storedCode)) {
            return userService.mailAndcode(vo);
        } else {
            return RestBean.failure(400,"验证码输入错误");
        }
    }

    //通过手机修改密码
    @RequestMapping("Changepassword")
    public ResponseDTO Changepassword(@RequestBody LoginVo vo) {
        String storedCode = (String) redisTemplate.opsForValue().get("code_" + vo.getPhoneNumber());
        if (storedCode != null && vo.getCode().equals(storedCode)) {
            return userService.Changepassword(vo);
        } else {
            return ResponseDTO.error("验证码输入错误");
        }
    }

    //    绑定邮箱及更改邮箱
    @RequestMapping("bindemail")
    public ResponseDTO bindemail(@RequestBody LoginVo vo) {
        // 从Redis中获取存储的验证码
        String storedCode = (String) redisTemplate.opsForValue().get("emailcode" + vo.getEmail());
        // 检查存储的验证码是否存在以及是否与用户输入的验证码匹配
        if (storedCode != null && vo.getCode().equals(storedCode)) {
            return userService.bindemail(vo);
        } else {
            return ResponseDTO.error("验证码输入错误");
        }
    }

    //    更改个人信息(名字、密码、个性签名、头像)
    @RequestMapping("ChangeInformation")
    public ResponseDTO ChangeInformation(@RequestBody Users users) {
        return userService.ChangeInformation(users);
    }

    //注销用户
    @RequestMapping("LogoutUser")
    public ResponseDTO LogoutUser(@RequestBody LoginVo vo) {
        return userService.LogoutUser(vo);
    }

    //根据用户id返回用户信息
    @RequestMapping("FindUser")
    public RestBean<Users> FindUser( String id) {
        System.out.println("**FindUser***"+id);
        return userService.FindUser(id);
    }

    //判断双向用户黑名单状态
    @RequestMapping("UserisBlocked")
    public ResponseDTO UserisBlocked(@RequestBody LoginVo vo) {
        return userService.isBlocked(vo);
    }


    //用户拉黑
    @RequestMapping("isBlocked")
    public ResponseDTO isBlocked(@RequestBody LoginVo vo) {
        return userService.addBlock(vo);
    }

    //用户注销清除Token
    @RequestMapping("quitUser")
    public RestBean quitUser(String id) {
        return userService.quitUser(id);
    }

}



